import axios from "axios";
import { baseUrl } from "./util.js";
export var token = null;

export async function try_login(username, password) {
    let ret;
    try {
        console.log(baseUrl + "account/login");
        console.log(username);
        console.log(password);
        const resp = await axios({
            method: 'post',
            url: baseUrl + "account/login",
            params: {
                username: username,
                password: password
            }
        })
        //resp = await.resp;
        console.log(resp);
        if (resp.data.error_message === "success") {
            console.log("登陆成功");
            token = resp.data.token;
            ret = true;
        } else {
            ret = false;
            console.log("登陆失败");
        }
    } catch (e) {
        ret = false;
        console.log("登陆失败");
    }
    return ret;
    //const respond = axios.post(baseUrl + "account/login", { username: username, password: password });
    // var t = await respond;
    // if (t.data.success) {
    //     // 成功
    //     console.log("登陆成功");
    //     token = t.data.token;
    //     ret = true;
    // } else {
    //     ret = false;
    // }
}


export async function try_register(username, password, confirmedPassword) {
    let ret;
    try {
        console.log(baseUrl + "account/register");
        console.log(username);
        console.log(password);
        const resp = await axios({
            method: 'post',
            url: baseUrl + "account/register",
            params: {
                username: username,
                password: password,
                confirmedPassword
            }
        })
        //resp = await.resp;
        console.log(resp);
        if (resp.data.error_message === "success") {
            console.log("注册成功");
            return true;
        } else {
            console.log("注册失败");
            return false;
        }
    } catch (e) {
        ret = false;
        console.log("注册失败");
        return false;
    }
    return ret;
}

export async function comment(movieId, stars, liking) {
    axios.post(baseUrl + `${movieId}`, { stars, liking, token })
}